package fr.sii.android.accessibility;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

import com.example.webservcicetest.R;

import fr.sii.android.accessibility.model.Configuration;
import fr.sii.android.accessibility.model.MapEntry;

/**
 * An activity representing a list of Items. This activity has different
 * presentations for handset and tablet-size devices. On handsets, the activity
 * presents a list of items, which when touched, lead to a
 * {@link ItemDetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two
 * vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link ItemListFragment} and the item details (if present) is a
 * {@link ItemDetailFragment}.
 * <p>
 * This activity also implements the required {@link ItemListFragment.Callbacks}
 * interface to listen for item selections.
 */
public class ItemListActivity extends Activity implements
		ItemListFragment.Callbacks {

	// ////////RESOURCES/////////////////
	/**
	 * Properties
	 */
	Configuration c;

	/**
	 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
	 * device.
	 */
	private boolean mTwoPane;

	// /////////FUNCTIONS///////////////

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// load config
		try {
			c = new Configuration("AVIP.properties");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// init
		try {
			c.initConfig("AVIP.properties");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// TEST
		try {
			Log.d("Test properties",
					c.getConfig("AVIP.properties", "Geolocalisation"));
			System.out.println(c
					.getConfig("AVIP.properties", "Geolocalisation"));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		requestWindowFeature(Window.FEATURE_PROGRESS);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

		setContentView(R.layout.activity_item_list);

		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayShowTitleEnabled(false);

		ActionBar.TabListener listener1 = new Tab1Listener();
		Tab tab1 = actionBar.newTab().setText(R.string.title_tab1)
				.setTabListener(listener1);
		// if version is advanced enough (this function is not supported in
		// lower versions)
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			tab1.setContentDescription(R.string.title_tab1);
		}
		actionBar.addTab(tab1);
		ActionBar.TabListener listener2 = new Tab2Listener();
		Tab tab2 = actionBar.newTab().setText(R.string.title_tab2)
				.setTabListener(listener2);
		// if version is advanced enough (this function is not supported in
		// lower versions)
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			tab2.setContentDescription(R.string.title_tab2);
		}
		actionBar.addTab(tab2);
		actionBar.setTitle(R.string.app_name);
		actionBar.setDisplayShowTitleEnabled(true);
		if (findViewById(R.id.item_detail_container) != null) {
			// The detail container view will be present only in the
			// large-screen layouts (res/values-large and
			// res/values-sw600dp). If this view is present, then the
			// activity should be in two-pane mode.
			mTwoPane = true;

			// In two-pane mode, list items should be given the
			// 'activated' state when touched.
			((ItemListFragment) getFragmentManager().findFragmentById(
					R.id.item_list)).setActivateOnItemClick(true);
		}

		// TODO: If exposing deep links into your app, handle intents here.
	}

	/**
	 * Callback method from {@link ItemListFragment.Callbacks} indicating that
	 * the item with the given ID was selected.
	 */
	@Override
	public void onItemSelected(MapEntry entry) {
		if (mTwoPane) {
			// In two-pane mode, show the detail view in this activity by
			// adding or replacing the detail fragment using a
			// fragment transaction.
			Bundle arguments = new Bundle();
			arguments.putString(ItemDetailFragment.ARG_NAME, entry.name);
			arguments.putString(ItemDetailFragment.ARG_LATITUDE, entry.lat);
			arguments.putString(ItemDetailFragment.ARG_LONGITUDE, entry.lon);
			arguments.putString(ItemDetailFragment.ARG_URL, entry.url);

			ItemDetailFragment fragment = new ItemDetailFragment();
			fragment.setArguments(arguments);
			getFragmentManager().beginTransaction()
					.replace(R.id.item_detail_container, fragment).commit();

		} else {
			// In single-pane mode, simply start the detail activity
			// for the selected item ID.
			Intent detailIntent = new Intent(this, ItemDetailActivity.class);
			detailIntent.putExtra(ItemDetailFragment.ARG_NAME, entry.name);
			detailIntent.putExtra(ItemDetailFragment.ARG_LATITUDE, entry.lat);
			detailIntent.putExtra(ItemDetailFragment.ARG_LONGITUDE, entry.lon);
			detailIntent.putExtra(ItemDetailFragment.ARG_URL, entry.url);
			startActivity(detailIntent);
		}
	}

	public class Tab1Listener implements ActionBar.TabListener {
		
		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			Fragment fragment = getFragmentManager().findFragmentById(R.id.item_list);
			ft.show(fragment);
			if (mTwoPane) {
				findViewById(R.id.item_detail_container).setVisibility(View.VISIBLE);
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			Fragment fragment = getFragmentManager().findFragmentById(R.id.item_list);
			ft.hide(fragment);
			if (mTwoPane) {
				findViewById(R.id.item_detail_container).setVisibility(View.INVISIBLE);
			}
		}

	}
	
	public class Tab2Listener implements ActionBar.TabListener {

		private Fragment mFragment;
		
		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			if (mFragment == null) {
				mFragment = Fragment.instantiate(ItemListActivity.this, MapFragment.class.getName());
				ft.add(android.R.id.content, mFragment);
			}
			else {
				ft.attach(mFragment);
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			if (mFragment != null) {
				ft.detach(mFragment);
			}
		}

	}
}
